package com.ch.light.web.service.impl;


import com.ch.light.web.model.Role;
import com.ch.light.web.model.User;
import com.ch.light.web.service.RoleService;
import com.ch.light.web.service.UserService;
import com.ch.utils.CommonUtils;
import com.google.common.base.Strings;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.ch.light.web.dao.UserRoleDao;
import com.ch.light.web.entity.UserRole;
import com.ch.light.web.service.UserRoleService;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleDao userRoleDao;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    @Override
    public List<UserRole> getListByUserId(String userId) {
        return userRoleDao.find("getListByUserId", userId);
    }

    @Deprecated
    @Override
    public List<String> getRoleIdsByUserId(String userId) {
        return userRoleDao.findColumn("getRoleIdsByUserId", String.class, userId);
    }

    @Override
    public void add(String[] roleIds, String userId) {
        checkArgument(!Strings.isNullOrEmpty(userId), "用户编号不能为空");
        userRoleDao.delete("deleteByUserId", userId);
        if (!Objects.isNull(roleIds) && roleIds.length > 0) {
            for (String roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRoleDao.save(userRole);
            }
        } else {
            userRoleDao.delete("deleteByUserId", userId);
        }
    }

    @Transactional
    @Override
    public void assign(String userId, String[] roleIdArr) {
        checkArgument(!Strings.isNullOrEmpty(userId), "用户编号不能为空");
        User user = userService.getByUserId(userId);
        checkNotNull(user, "用户对象不存在");
        List<Role> ownRoles = roleService.findByUserId(userId);
        List<String> addRoleIds = Lists.newArrayList();
        List<String> delRoleIds = Lists.newArrayList();
        if (CommonUtils.isNotEmpty(ownRoles)) {
            List<String> ownRoleIds = ownRoles.stream().map(Role::getNo).collect(Collectors.toList());
            if (!CommonUtils.isEmpty(roleIdArr)) {
                addRoleIds = Lists.newArrayList(roleIdArr);
                delRoleIds = Lists.newArrayList(ownRoleIds);
                //add user to role
                delRoleIds.removeAll(addRoleIds);
                //delete user to role
                addRoleIds.removeAll(ownRoleIds);
            }
        } else if (!CommonUtils.isEmpty(roleIdArr)) {
            addRoleIds = Lists.newArrayList(roleIdArr);
        }
        if (CommonUtils.isNotEmpty(addRoleIds)) {
            userService.assignRoles(userId, addRoleIds);
        }
        if (CommonUtils.isNotEmpty(delRoleIds)) {
            userService.reassignRoles(userId, delRoleIds);
        }
    }
}
